clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
* ---------------------------------------------- *
*compute aggregate LTG
use if LTG<. using $Work/ibes_ret, clear
gstats winsor LTG, cuts(1 99) by(datem) replace

bys datem: egen Tot=total(mcap)
gen vw=LTG*(mcap/Tot)

collapse (sum) LTG=vw, by(datem)
replace LTG=. if LTG==0

tsset datem

gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen dMoLLTG=LTG -LMLTG
gen dQrLLTG=LTG -LQLTG

forvalues x=1(1)5 { 
local t=12*`x'
gen  L`x'LTG=L`t'.LTG
gen  d`x'LLTG=LTG-L`x'LTG
}

foreach var in *LTG {
	rename `var' `var'agg
}

keep datem *agg
tempfile LTGagg
save "`LTGagg'", replace
* ************************************************
* generated by EBR_Monthly.do
use permco permno datem LTG medest* *FE* cp* EPS*Yr EPS book* mcap op inv BOOK INV PROF MOME SI* qruf1 cfacpr prc LTG *STG* using $Work/EBR_new_monthly, clear
keep if qruf1<.
drop SIZE
rename SI10 SIZE

tsset permno datem
gen LMLTG=L1.LTG
gen LQLTG=L3.LTG
gen L1LTG=L12.LTG
gen L3LTG=L36.LTG
gen L5LTG=L60.LTG

gen LMSTG=L1.STG2
gen LQSTG=L3.STG2
gen L1STG=L12.STG2
gen L3STG=L36.STG2
gen L5STG=L60.STG2

gen dMoLLTG=LTG -L1.LTG
gen dQrLLTG=LTG -L3.LTG

gen dMoLSTG=STG2-L1.STG2
gen dQrLSTG=STG2-L3.STG2

tsset permno datem
forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
	gen d`x'LSTG=STG2-L`j'.STG2

}
gen LFE1=L12.FE1
gen LFE3=L36.FE3	
gen LFE5=L60.FE5
* ************************************************
merge 1:1 permco permno datem using $Data/read_compustat, keepusing(permco permno datem sale bookeq L6mcap)
drop if _merge==2
drop _merge
* ************************************************
tsset permno datem
foreach var of varlist sale {
	replace `var'=L.`var' if `var'==. & month(dofm(datem))!=6
}
replace L6mcap=L6.mcap if L6mcap==. 

replace L6mcap=. if bookeq==. | bookeq<0
replace bookeq=. if L6mcap==. | bookeq<0

gen lnLFE1=ln(EPS)/1-ln(L12.medest12)/1		if L12.medest12>0 
gen lnLFE3=ln(EPS)/3-ln(L36.medest36)/3		if L36.medest12>0 
gen lnLFE5=ln(EPS)/5-ln(L60.EPS)/5-L60.LTG	if L60.LTG>-1 

gstats winsor *FE* lnLF* cp* *LTG *STG* sale op inv, cuts(1 99) by(datem) replace

tempfile june
save "`june'", replace
* ***************
foreach p in SIZE INV PROF MOME {
use if `p'<. using "`june'", clear
gen portf=1+int((`p'-1)/2)
gen d1LSTG2=STG2-L1STG2

collapse (mean)     *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* op inv (median) sale, by(datem portf)
gen lnsale=ln(sale)
drop sale 
/* ///////////////////////////////////////////////// */
* GENERATE HML SPREADS for variables other than returns
preserve
keep if portf==1 | portf==5  
foreach var of varlist *LTG STG2 *STG LFE1 LFE3 LFE5 lnLFE* lnsale op inv {
	replace `var'=-`var' if portf==5 & index("`p'","INV")+index("`p'","SIZE")==1
	replace `var'=-`var' if portf==1 & index("`p'","INV")+index("`p'","SIZE")==0
}	
foreach v of varlist  LTG L*LTG d*LTG STG2 L*STG d*STG LFE1 LFE3 LFE5 lnLFE* lnsale op inv {
bysort datem: egen count`v' = count(`v') 
}
collapse (sum)         LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* lnsale op inv (mean) count*, by(datem)
foreach v of varlist   LTG L*LTG d*LTG STG2 L*STG d*STG  lnLFE* lnsale op inv  {
replace `v'=. if count`v'!=2
}

generate portf=6

tempfile PORTF
save "`PORTF'", replace

restore
append using "`PORTF'"
merge 1:1 datem portf using $Work/portfolio_`p'_qnic, keepusing(portf datem cum* L12ret lnDP L*ret*)
drop _merge
rename lnDP lndp
merge m:1 datem using "`LTGagg'"
drop if _merge<3
drop _merge  

tsset portf datem
keep if L1LTG+d1LLTG+STG2+d1LLTGagg+L1LTGagg+lnLFE5<.
local Z "d*LLTG d*LSTG d*LLTG L*LTG L*STG lnLFE* *agg"   

* STANDARRIZE THE VARIABLES
foreach var of varlist Lret* lndp `Z' {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}
* ************************************************
* Regressions using predicted EBRs
* ************************************************
gen lnpd=-lndp
eststo clear
generate DLT=dMoLLTG
generate DST=dMoLSTG
generate LLT=LMLTG
generate LST=LMSTG
generate LFE=1
generate DLTa=dMoLLTGagg
generate LLTa=LMLTGagg
* *******************
eststo clear
	quietly: ivreg2 cumQRetMo1 lnpd DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(1 ) robust small
predict hat if e(sample)
eststo: quietly: ivreg2 cumNRetMo1 lnpd hat					if portf==6, bw(1 ) robust small
drop hat

replace LFE=lnLFE1
replace DLT=d1LLTG
replace DST=d1LSTG
replace LLT=L1LTG
replace LST=L1STG
replace DLTa=d1LLTGagg
replace LLTa=L1LTGagg
	quietly: ivreg2 cumQRetYr1 lnpd DLT LLT DST LST LFE DLTa LLTa		if portf==6, bw(13) robust small
predict hat if e(sample)
eststo: quietly: ivreg2 cumNRetYr1 lnpd hat					if portf==6, bw(13) robust small
drop hat
* **************************************
esttab 					, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) order(hat lnpd) 
if "`p'"=="SIZE" {
esttab using $Tables/Table08_dp_June.csv, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) order(hat lnpd)  title("`p'") replace
}
if "`p'"!="SIZE" {
esttab using $Tables/Table08_dp_June.csv, nonotes  compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) order(hat lnpd)  title("`p'") append
}
}
